package com.yixun.qfbao.hander;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.apidto.ModelReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.ModelService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.SaveModelReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	保存经纪人注册自定义模板
 */
@Service("saveAgentRegesiterModelHander")
public class SaveModelHander extends CommonAbstractHander{

	@Autowired
	ModelService modelService;
	
	@Autowired
	LoginUtils loginUtils;
	
	@Override
	@SuppressWarnings("rawtypes")
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		SaveModelReqVo reqVo = (SaveModelReqVo) reqDto;
		
		String userId = reqVo.getParams().getUserId();
		PartnerInfo partnerInfo = loginUtils.getUser(userId);
		BaseResponseDTO res = new BaseResponseDTO();
		if(partnerInfo == null) {
			res.setErrorMsg("未登录");
			res.setResult(ResultEnum.FAIL.getNo());
			return res;
		}
		
		Long companyId = partnerInfo.getHigherid();
		
		List<ModelReqDto> modelReqList = reqVo.getParams().getModelReqDtoList();
		
		List<Model> modelList = new ArrayList<>();
		for (ModelReqDto modelReqDto : modelReqList) {
			Model model = new Model();
			model.setName(modelReqDto.getName());
			model.setType(StringUtil.isNotNull(modelReqDto.getType())? 
					modelReqDto.getType(): ModelTypeEnum2.PARTNER_CHECK.getNo());
			model.setDisplayType(modelReqDto.getDisplayType());
			model.setDisplayStatus(modelReqDto.getDisplayStatus());
			model.setRequiredStatus(modelReqDto.getRequiredStatus());
			modelList.add(model);
		}
		
		int result = modelService.reAddModel(companyId, userId, modelList);
		
		
		res.setResData(result == modelList.size()? "保存成功": "保存失败");
		res.setResult(result == modelList.size()? ResultEnum.SUCCESS.getNo(): ResultEnum.FAIL.getNo());
		return res;
	}

}
